package datos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import conexion.ConexionBD;

public class ConsultaUsuario extends Consulta {

	ResultSet res;
	PreparedStatement pstm;
	ArrayList<String> r;

	public ResultSet getEmpleado(String idEmpl) throws SQLException {
		pstm = conect.getConexion().prepareStatement(
				"SELECT" + " idempleado," + " iddepartamento," + " idcargo,"
						+ " nombre," + " telefono, " + " cedula, "
						+ " direccion, " + " contrasena" + " FROM empleado"
						+ " WHERE idempleado = '" + idEmpl + "';");
		res = pstm.executeQuery();

		return res;
	}

	public ResultSet getEmpleadosDept(String idDept) throws SQLException {
		pstm = conect.getConexion().prepareStatement(
						"SELECT "
								+ "e.idempleado, e.iddepartamento, e.idcargo, e.nombre, "
								+ "d.iddepartamento, d.nombre "
								+ "FROM empleado e, departamento d "
								+ "WHERE d.iddepartamento = '" + idDept
								+ "and d.iddepartamento = e.iddepartamento");
		res = pstm.executeQuery();
		return res;
	}

	public ArrayList<String> getEmpleados() throws SQLException  {		
		int i = 0;
		r= new ArrayList<String>();
		String [] s=null;
		
		pstm = conect.getConexion().prepareStatement(
				"SELECT" + " idempleado," + " iddepartamento," + " idcargo,"
						+ " nombre," + " telefono, " + " cedula, "
						+ " direccion, " + " contrasena" + " FROM empleado;");
		res = pstm.executeQuery();
		while (res.next()) {			
			r.add(res.getString("nombre"));
			System.out.println(r.get(i));
			i++;
		}
		System.out.println("por aca pase");		
		
		return r;		

	}

	public ArrayList<String> getTareas(String idDept) throws SQLException  {		
		int i = 0;
		System.out.println(Integer.toString((Integer.parseInt(idDept)-1)));
		r= new ArrayList<String>();		
		pstm = conect.getConexion().prepareStatement(
				"SELECT *"+" FROM producto"+" WHERE idfase LIKE '%"+Integer.toString((Integer.parseInt(idDept)-1))+"%';");
		res = pstm.executeQuery();
		while (res.next()) {			
			r.add(res.getString("descproducto"));
			System.out.println(r.get(i));
			i++;
		}
		System.out.println("por aca pase");		
		
		return r;		

	}
	
	public String getCargoEmp(String id) {
		String re = null;
		try {
			pstm = conect.getConexion().prepareStatement(
					"SELECT idempleado, idcargo " + "FROM empleado "
							+ "WHERE idempleado = '" + id + "';");
			res = pstm.executeQuery();
			while (res.next()) {
				re = res.getString("idcargo");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return re;

	}
	
	public String getName(String id) throws SQLException{
		String name="";
		pstm = conect.getConexion().prepareStatement(
				"SELECT " +
				"nombre " +
				"FROM empleado " +
				"WHERE idempleado ='"+id+"';");
		res = pstm.executeQuery();
		while(res.next()){
			 name = res.getString("nombre");
		}
		return name;
	}
	
	public int getNumeroRegistros() throws SQLException{
		int n=0;
		pstm = conect.getConexion().prepareStatement(
				"SELECT count(*) FROM empleado;");
		res = pstm.executeQuery();
		while(res.next()){
		System.out.println(" "+res.getInt(1));
		n = res.getInt(1);
		}
		return n;
		}
	



	
	public String getDepartamento(String id) {
		String re = null;
		try {
			pstm = conect.getConexion().prepareStatement(
					"SELECT idempleado, idcargo, iddepartamento " + "FROM empleado "
							+ "WHERE idempleado = '" + id + "';");
			res = pstm.executeQuery();
			while (res.next()) {
				re = res.getString("iddepartamento");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return re;

	}

}
